package com.iflytek.universitymanagement.utils;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.iflytek.universitymanagement.domain.Student;
import com.iflytek.universitymanagement.mapper.StudentMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * Token工具类
 */
@Component
public class TokenUtils {

   /* private static final Logger log = LoggerFactory.getLogger(TokenUtils.class);

    private static StudentMapper staticStudentMapper;

    @Resource
    private StudentMapper studentMapper;

    @PostConstruct
    public void setUserService() {
        staticStudentMapper = studentMapper;
    }

    *//**
     * 生成token
     *//*
    public static String createToken(String studentId, String sign) {
        return JWT.create().withAudience(studentId) // 将 userid 保存到 token 里面,作为载荷
                .withExpiresAt(DateUtil.offsetHour(new Date(), 2)) // 2小时后token过期
                .sign(Algorithm.HMAC256(sign)); // 以 password 作为 token 的密钥
    }

    *//**
     * 获取当前登录的用户信息
     *//*
    public static Student getCurrentUser() {
        try {
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            String token = request.getHeader("token");
            if (StrUtil.isNotBlank(token)) {
                String studentId = JWT.decode(token).getAudience().get(0);
                return staticStudentMapper.selectById(Integer.valueOf(studentId));
            }
        } catch (Exception e) {
            return null;
        }
        return null;  // 返回空的账号对象
    }*/
}

